package br.ufrj.neg.servlet;

import java.io.IOException;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.velocity.Template;
import org.apache.velocity.context.Context;
import org.apache.velocity.tools.view.VelocityViewServlet;


import br.ufrj.neg.constants.General;
import br.ufrj.neg.preparacao.bean.NegociacaoContext;
import br.ufrj.neg.preparacao.model.NegociacaoModel;
import br.ufrj.neg.usuario.bean.UsuarioBean;

public class ListarNegociacaoAction extends VelocityViewServlet{

	private static final long serialVersionUID = 3759364658257960936L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		NegociacaoModel nm = new NegociacaoModel();

		HttpSession session = request.getSession(false);
		
		UsuarioBean ub = null;
		synchronized(session){
			session.removeAttribute("NegociacaoBean");
			ub=(UsuarioBean)session.getAttribute("usuarioBean");
		}
		
		
		NegociacaoContext negociacaoContext = new NegociacaoContext();
		negociacaoContext.getNegociacao();
		negociacaoContext.setDataSet(nm.listaNegociosPorUsuario(ub.getId()));
		
		setContentType(request, response);
		
		Context context = createContext(request, response);
		fillContext(context, request);
		
		context.put("pageContext",new PageContext(request));
		context.put("negociacaoContext",negociacaoContext);
			
		Template template;
		
		if(ub.getPapel()==2){
			template = getTemplate(General.TEMPLATE_FUZZY_PATH + "listaNegocio.vm");
		}else if(ub.getPapel()==4){
			template = getTemplate(General.TEMPLATE_PREPARACAO_PATH + "listaNegocio.vm");
		}else if(ub.getPapel()==5){
			template = getTemplate(General.TEMPLATE_RISCO_PATH + "listaNegocio.vm");
		}else if(ub.getPapel()==6){
			template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "listaNegocio.vm");
		}else{
			template = getTemplate(General.TEMPLATE_REUNIAO_PATH + "listaNegocio.vm");
		}
		
		
		mergeTemplate(template, context, response);

	}
}
